package com.foreveredu.erlms.web.struts.action;

import java.awt.Font;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;

import com.foreveredu.erlms.domain.Department;
import com.foreveredu.erlms.domain.Parameter;
import com.foreveredu.erlms.service.IExamService;
import com.foreveredu.erlms.service.IManagementService;
import com.foreveredu.erlms.web.struts.actionform.ParameterActionForm;

/*
 * @author hushigang 2012/4/16
 * DataIOAction负责处理数据的导入导出请求
 */
public class ParameterAction extends AbstractBaseDispatchAction {

	private IExamService examService = null;
	
	public IExamService getExamService() {
		return examService;
	}
	
	public void setExamService(IExamService examService) {
		this.examService = examService;
	}
	
	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}
	public ActionForward setParams(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		ParameterActionForm paf = (ParameterActionForm)form;
		
		//获得当前系统参数信息
		Parameter param = null;
		List<Parameter> pList = examService.findParameters();
		if (pList != null && pList.size() > 0) {
			param = pList.get(0);
			
			//将当前系统参数显示在页面中
			PropertyUtils.copyProperties(paf, param);
		}
				
		return mapping.findForward("setParams");
	}
	
	public ActionForward updateParams(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		ParameterActionForm paf = (ParameterActionForm)form;
		Parameter param = examService.findParameterById(paf.getId());
		
		//如果修改考试届次，则将MaxSNRecord表中记录的最大网报号和最大报名号置为1
		if (examService.getExamOrderOfNow() != paf.getExamOrder()) {
			examService.clearMaxSNRecord();
		}
			
		PropertyUtils.copyProperties(param, paf);
		
		List<Parameter> pList = examService.findParameters();
		if (pList != null && pList.size() > 0) {
			//修改系统参数
			examService.updateParameter(param);
		} else {
			//如果系统当前未设置系统参数，则插入系统参数
			examService.saveParameter(param);
		}
		
		return mapping.findForward("updateParams");
	}
	
	//显示各个考次成绩分析图
	public ActionForward parameterImg(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		//获取所有的考次列表
		List<Integer> examOrderList = new ArrayList<Integer>();
		List<Parameter> parameters = new ArrayList<Parameter>();
		examOrderList = examService.findExamOrderByExaminee();
		
		for(Iterator<Integer> examOrder =examOrderList.iterator(); examOrder.hasNext();){
			Parameter parameter =new Parameter();
			parameter.setExamOrder(examOrder.next());
			parameters.add(parameter);
		}
		request.setAttribute("parameters", parameters);
		
		ParameterActionForm paf = (ParameterActionForm)form;
		
		int examOrder = paf.getExamOrder();
		
		List<Department> deptList = managementService.findDepartments();		

		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		
		for (Iterator<Department> departments = deptList.iterator();departments.hasNext();) {
			Department department =departments.next();
			//查找出该院系下不及格学生人数
			int gradeOverNum = examService.findGradeByDeptIdAndZcj(department.getId(),examOrder);
			int gradeAllNum = examService.findGradeByDeptId(department.getId(),examOrder);
			if(gradeAllNum == 0){
				dataset.addValue(0, "", department.getDeptName());
			}else{
				int gradeScale=(int)(((gradeAllNum - gradeOverNum)/(double)gradeAllNum)*100);
				dataset.addValue(gradeScale, "", department.getDeptName());
			}
		}
		
		JFreeChart chart = ChartFactory.createBarChart3D("各院系过级比例图", " ",
				"过级率百分比", dataset, PlotOrientation.VERTICAL, false, false,
				false);
		CategoryPlot plot=chart.getCategoryPlot();
		ValueAxis rangeAxis=plot.getRangeAxis();//获取柱状  
		rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,20));  

//		chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
		//设置标题字体
		chart.getTitle().setFont(new Font("宋体",Font.BOLD,20));
		
//		chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);

		
		BarRenderer3D renderer = new BarRenderer3D();
		renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); 
		renderer.setBaseItemLabelsVisible(true); 
		renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT)); 
		renderer.setItemLabelAnchorOffset(10D); 
		plot.setRenderer(renderer); 
		
		CategoryAxis domainAxis = plot.getDomainAxis();  
		domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,11));// X轴字体    

		
		String filename = ServletUtilities.saveChartAsPNG(chart,1000,520,null);
		String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;
		request.setAttribute("filename", filename);
		request.setAttribute("graphURL", graphURL);
		return mapping.findForward("parameterImg");
	}

}
